import java.io.BufferedReader;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.util.Scanner;

/*
 Write a program called GradesHistogram, which reads in n grades (of int between 0 and 100, inclusive) 
 from a text file called "grades.in" and displays the histogram horizontally and vertically. 
 The file has the following format:

numStduents:int
grade1:int grade2:int .... gradeN:int

For example:
15
49 50 51 59 0 5 9 10 15 19 50 55 89 99 100


output:
  0 -  9: ***
 10 - 19: ***
 20 - 29: 
 30 - 39: 
 40 - 49: *
 50 - 59: *****
 60 - 69: 
 70 - 79: 
 80 - 89: *
 90 -100: **
 */


public class GradesHistogram{

	
	
	public static int[] grades;
	public static int[] bins = new int[10];
     // Declare and allocate an int array for histogram bins.
     // 10 bins for 0-9, 10-19,...., 90-100
  
	
	public static String histogram(String fileName) throws FileNotFoundException{
		readGrades("grades.in");
	    computeHistogram();
	    return printHistogramHorizontal();		
	}
	
	  // Read the grades from "filename", store in "grades" array.
	   // Assume that the inputs are valid.
	   public static void readGrades(String filename) throws FileNotFoundException {
		   Scanner in = new Scanner(new File("src/grades.in"));
		   int numOfStudents = 0;
		   if(in.hasNextInt()) 
			   numOfStudents = in.nextInt();
		   grades = new int[numOfStudents];
		   int index=0;
		   while(in.hasNextInt()){
			   grades[index] = in.nextInt();
			   index++;
		   }
		   in.close();
	   }

	   // Based on "grades" array, populate the "bins" array.
	   public static void computeHistogram() {
		   for(int k=0;k<bins.length;k++){
			   bins[k] = 0;
		   }
		   for(int i =0;i<grades.length;i++){
			   if(grades[i]==100){
				   bins[9]++;
			   }else{				   
				   bins[grades[i]/10]++;
			   }
		   }
	   }

	   // Print histogram based on the "bins" array.
	   public static String printHistogramHorizontal() {
		   String result = "";
		   String[] gradeRanges = {  
				   " 0 -  9: ",
				   "10 - 19: ",
				   "20 - 29: ",
				   "30 - 39: ",
				   "40 - 49: ",
				   "50 - 59: ",
				   "60 - 69: ",
				   "70 - 79: ",
				   "80 - 89: ",
				   "90 -100: "};
		   
		   for(int j=0;j<bins.length;j++){
			   System.out.print(gradeRanges[j]);
			   result+=gradeRanges[j];
			   for(int asterisk = 0;asterisk<bins[j];asterisk++){
				   System.out.print("*");
				   result+="*";
			   }
			   if(j<bins.length-1){
				   System.out.print("\n");
				   result+="\n";				   
			   }
		   }
		   return result;
	   }

	
}











